{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medical image classification\n",
    "\n",
    "*Copyright (c) 2022 Institute for Quantum Computing, Baidu Inc. All Rights Reserved.*\n",
    "\n",
    "Medical image classification is the key technology of computer-aided diagnosis systems. The problem of medical image classification is how to extract features from images and classify them, so as to identify and understand which parts of the human body are affected by specific diseases. Here, we mainly use a quantum neural network to classify the chest data in the open data set MedMNIST, which has the following form\n",
    "\n",
    "<img src=\"./med_image_example.png\" width=\"20%\" height=\"20%\"/>\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## QNNMIC model for medical image classification\n",
    "\n",
    "### Introduction of QNNMIC\n",
    "QNNMIC model is a quantum machine learning (QML) model that can be used for medical image classification. We specifically call it a quantum neural network (QNN), which combines parameterized quantum circuit (PQC) and a classical neural network. For medical image data, QNNMIC can achieve more than 85% classification accuracy. The model is mainly divided into quantum and classical parts. The structure diagram is as follows:\n",
    "\n",
    "<img src=\"./qnnmic_model_en.png\" width=\"60%\" height=\"60%\"/>\n",
    "\n",
    "\n",
    "Remarks：\n",
    "- In general, we use principal component analysis (PCA) to reduce the dimension of the image data, making it easier to encode classical data into quantum states through coding circuits.\n",
    "- The parameterized circuit is used for feature extraction, and its circuit parameters can be adjusted during training.\n",
    "- Quantum measurement, represented by a set of measurement operators, is the process of converting quantum states into classical data, which can be further processed.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quick start\n",
    "\n",
    "### Use the model to make predictions\n",
    "\n",
    "Here, we have given a trained model saved in the format `qnnmic.pdparams` which can be directly used to distinguish medical images. One only needs to do the corresponding configuration in this file `test.toml`, and enter the command `python qnn_medical_image.py --config test.toml` to predict the input images.\n",
    "\n",
    "### Online Test\n",
    "\n",
    "The following shows how to configure the test file `test_toml` to make medical image prediction."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import toml\n",
    "\n",
    "test_toml = r\"\"\"\n",
    "# The config for testing the QNNMIC model.\n",
    "\n",
    "# The path of the input image.\n",
    "image_path = 'pneumoniamnist'\n",
    "\n",
    "# The number of data in the test dataset.\n",
    "# The value defaults to -1 which means using all data.\n",
    "num_samples = 20\n",
    "\n",
    "# The path of the trained model, which will be loaded.\n",
    "model_path = 'qnnmic.pdparams'\n",
    "\n",
    "# The number of qubits of the quantum circuit in each layer.\n",
    "num_qubits = [8, 8]\n",
    "\n",
    "# The depth of the quantum circuit in each layer.\n",
    "num_depths = [2, 2]\n",
    "\n",
    "# The observables of the quantum circuit in each layer.\n",
    "observables = [['Z0','Z1','Z2','Z3'], ['X0','X1','X2','X3']]\n",
    "\"\"\"\n",
    "\n",
    "config = toml.loads(test_toml)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The prediction results of the input images are 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0 respectively.\n",
      "The labels of the input images are 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0 respectively.\n"
     ]
    }
   ],
   "source": [
    "from paddle_quantum.qml.qnnmic import inference\n",
    "\n",
    "prediction, prob, label = inference(**config)\n",
    "print(f\"The prediction results of the input images are {str(prediction)[1:-1]} respectively.\")\n",
    "print(f\"The labels of the input images are {str(label)[1:-1]} respectively.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here label 0 means abnormal lungs, and label 1 means normal lungs."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In `test_toml` :\n",
    "- `model_path`: this is the trained model, here we set it as `qnnmic.pdparams`.\n",
    "- `num_qubits`, `num_depths`, `observables` these parameters correspond to the model ``qnnmic.pdparams``, `num_qubits = [8,8]` represents the quantum part of a total of two layers of circuit, each layer of the circuit has 8 qubits; `num_depths = [2,2]` represents the depth of parameterized circuit of each layer is 2；`observables` is the specific form of the measurement operator at each layer.\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For an abnormal image from the dataset\n",
    "\n",
    "<img src=\"./medical_image/image_label_0.png\" width=\"20%\" height=\"20%\"/>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For this input image, the model can detect the abnormality of the lung with 98.30% confidence.\n"
     ]
    }
   ],
   "source": [
    "# Use the model to make predictions and get the corresponding probability\n",
    "msg = f'For this input image, the model can detect the abnormality of the lung with {prob[10][1]:3.2%} confidence.'\n",
    "print(msg)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Remarks\n",
    "\n",
    "- We usually consider adjusting three hyperparameters，`num_qubits`, `num_depths` and `observables`, which have a greater impact on the model."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Reference information\n",
    "\n",
    "```\n",
    "@article{medmnistv2,\n",
    "    title={MedMNIST v2: A Large-Scale Lightweight Benchmark for 2D and 3D Biomedical Image Classification},\n",
    "    author={Yang, Jiancheng and Shi, Rui and Wei, Donglai and Liu, Zequan and Zhao, Lin and Ke, Bilian and Pfister, Hanspeter and Ni, Bingbing},\n",
    "    journal={arXiv preprint arXiv:2110.14795},\n",
    "    year={2021}\n",
    "}\n",
    "```\n",
    "\n",
    "## Reference\n",
    "\n",
    "\\[1\\] Yang, Jiancheng, et al. \"Medmnist v2: A large-scale lightweight benchmark for 2d and 3d biomedical image classification.\" arXiv preprint arXiv:2110.14795 (2021)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "modellib",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.15 (default, Nov 24 2022, 18:44:54) [MSC v.1916 64 bit (AMD64)]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "dfa0523b1e359b8fd3ea126fa0459d0c86d49956d91b464930b80cba21582eac"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
